package day37.edu;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

// Created: 07/10/2025, ChangJiang Ru
public class Exer2 {
    public static void main(String[] args) {
        TreeSet<Employee> treeSet = new TreeSet<Employee>();

        Employee e1 = new Employee("Aom", 18, new MyDate(2007, 3, 13));
        Employee e2 = new Employee("AArry", 19, new MyDate(2006, 4, 14));
        Employee e3 = new Employee("Geek", 17, new MyDate(2008, 5, 15));
        Employee e4 = new Employee("Milk", 22, new MyDate(2003, 1, 15));
        Employee e5 = new Employee("MySQL", 21, new MyDate(2004, 2, 19));

        treeSet.add(e1);
        treeSet.add(e2);
        treeSet.add(e3);
        treeSet.add(e4);
        treeSet.add(e5);

        Iterator<Employee> iterator = treeSet.iterator();
        while(iterator.hasNext())
        {
            Employee employee = iterator.next();
            System.out.println(employee);
        }

        System.out.println("==========");

        Comparator<Employee> comparator = new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                int yearDistince = o1.getBirthday().getYear() - o2.getBirthday().getYear();
                if(yearDistince != 0)
                {
                    return yearDistince;
                }

                int monthDistince = o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
                if(monthDistince != 0)
                {
                    return monthDistince;
                }

                return o1.getBirthday().getDay() - o2.getBirthday().getDay();
            }
        };
    }

    @Test
    public void test1()
    {
        Comparator<Employee> comparator = new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                int yearDistince = o1.getBirthday().getYear() - o2.getBirthday().getYear();
                if(yearDistince != 0)
                {
                    return yearDistince;
                }

                int monthDistince = o1.getBirthday().getMonth() - o2.getBirthday().getMonth();
                if(monthDistince != 0)
                {
                    return monthDistince;
                }

                return o1.getBirthday().getDay() - o2.getBirthday().getDay();
            }
        };

        TreeSet<Employee> treeSet = new TreeSet<Employee>(comparator);

        Employee e1 = new Employee("Aom", 18, new MyDate(2007, 3, 13));
        Employee e2 = new Employee("AArry", 19, new MyDate(2008, 4, 14));
        Employee e3 = new Employee("Geek", 17, new MyDate(2008, 5, 15));
        Employee e4 = new Employee("Milk", 22, new MyDate(2008, 1, 15));
        Employee e5 = new Employee("MySQL", 21, new MyDate(2004, 2, 19));

        treeSet.add(e1);
        treeSet.add(e2);
        treeSet.add(e3);
        treeSet.add(e4);
        treeSet.add(e5);

        Iterator<Employee> iterator = treeSet.iterator();
        while(iterator.hasNext())
        {
            Employee employee = iterator.next();
            System.out.println(employee);
        }
    }
}
